import api from './impl/axios_api_instance'
import {BizError} from '../exception/exceptions'
import rest from './rest'
import {remoteLogin} from '../auth_util'
import {Toast} from 'vant'
import store from '@/repo/store/store'

function postApi (data) {
  if (!data) {
    console.error('post api data cannot be null')
    return
  }
  let reqData = data.requestData || {}
  data.requestData = JSON.stringify(reqData)
  data.userid = data.userid || store.getters.userid || '0'
  data.tsId = data.tsId || store.getters.hsId;

  return post(`${rest.apiHost}/newfcty/h5/jn/json.hx`, data).then(result => {
      if (result.resultCode === '000000') {
        return Promise.resolve(result)
      } else if (result.resultCode === '001060') {
        remoteLogin();
        return Promise.reject('remote login')
      } else if (result.resultCode === '001000') {
        return Promise.reject(result.errorInfo)
      } else {
        Toast(result.errorInfo)
        return Promise.reject(new BizError(result))
      }
    }
  )
}

export default postApi

export function post (url, data) {
  return api.post(url, data).then(res => res.data || {})
}

export function get (url) {
  return api.get(url).then(res => res.data || {})
}
